//
//  SPStartPageController.m
//  QADoctor
//
//  Created by 陈飞宇 on 17/3/30.
//  Copyright © 2017年 Spencer. All rights reserved.
//

#import "SPStartPageController.h"
#import "SPSlidingViewController.h"
#import "AppDelegate.h"
#define CFYUserDefaults [NSUserDefaults standardUserDefaults]
#import "LZLoginViewController.h"
#import "JNavigationController.h"
#import "JTabBarController.h"


@interface SPStartPageController ()
{
    NSTimer *_timer;
    NSInteger _countDown;

}
@property (weak, nonatomic) IBOutlet UIImageView *startImageView;

@property (weak, nonatomic) IBOutlet UIButton *timeButton;

@property (nonatomic,strong) LZUserInfo * userInfo;
@end

@implementation SPStartPageController

- (void)viewDidLoad {
    [super viewDidLoad];
    _userInfo = [LZUserInfo getUserInfo];
    _countDown = 5;
    [BaseRequest requestWithMethodResponseStringResult:TestURL paramars:nil paramarsSite:@"/Login/start" sucessBlock:^(id content) {
        NSDictionary * dict = [NSJSONSerialization JSONObjectWithData:content options:NSJSONReadingMutableContainers error:nil];
        if ([dict[@"code"]isEqualToString:@"1"]) {
            [self.startImageView sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@",IconURL,content[@"img"]]] placeholderImage:nil completed:^(UIImage * _Nullable image, NSError * _Nullable error, SDImageCacheType cacheType, NSURL * _Nullable imageURL) {
                
                self.timeButton.hidden = NO;
                [self startedCountDown];
            }];

        }else
        {
            [self startedCountDown];
        }
        
    } failure:^(NSError *error) {
        
        [self startedCountDown];
        
    }];


}
- (IBAction)SkipAdvertising {//跳过广告点击
    
    [_timer invalidate];
    _timer = nil;
    
        AppDelegate *delegate =(AppDelegate *)[UIApplication sharedApplication].delegate;
    BOOL isFirst = [CFYUserDefaults boolForKey:@"isFirst"];
    if (isFirst == YES) {
//        BOOL IsLogin = _userInfo.isLogin;
//        if (IsLogin == NO) {
//            LZLoginViewController *loginVc = [[LZLoginViewController alloc]init];
//            JNavigationController *nav = [[JNavigationController alloc]initWithRootViewController:loginVc];
//            delegate.window.rootViewController = nav;
//            
//        }else
        
            JTabBarController *tabBarVc = [[JTabBarController alloc]init];
            delegate.window.rootViewController = tabBarVc;
        
        
    }else
    {
        SPSlidingViewController *splitView = [[SPSlidingViewController alloc]init];
        delegate.window.rootViewController = splitView;

        
    }

    
  

}

- (void)startedCountDown
{
    _timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(closeedCountDown) userInfo:nil repeats:YES];
}
- (void)closeedCountDown {
    if (_countDown > 0) {
        [self.timeButton setTitle:[NSString stringWithFormat:@"%zdS 跳过", _countDown] forState:UIControlStateNormal];
        _countDown--;
    } else {
        [self SkipAdvertising];
    
    
    }
}

@end
